package com.ruoyi.web.controller.mes;


import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.kt.mes.domain.StockBin;
import com.kt.mes.service.IStockBinService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 料仓Controller
 *
 * @author lzc
 * @date 2021-01-26
 */
@RestController
@RequestMapping("/mes/Stockbin")
public class StockBinController extends BaseController
{
    @Autowired
    private IStockBinService stockBinService;

    /**
     * 查询料仓列表
     */
    @PreAuthorize("@ss.hasPermi('mes:Stockbin:list')")
    @GetMapping("/list")
    public TableDataInfo list(StockBin stockBin)
    {
        startPage();
        List<StockBin> list = stockBinService.selectStockBinList(stockBin);
        return getDataTable(list);
    }

    /**
     * 导出料仓列表
     */
    @PreAuthorize("@ss.hasPermi('mes:Stockbin:export')")
    @Log(title = "料仓", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StockBin stockBin)
    {
        List<StockBin> list = stockBinService.selectStockBinList(stockBin);
        ExcelUtil<StockBin> util = new ExcelUtil<StockBin>(StockBin.class);
        return util.exportExcel(list, "Stockbin");
    }

    /**
     * 获取料仓详细信息
     */
    @PreAuthorize("@ss.hasPermi('mes:Stockbin:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(stockBinService.selectStockBinById(id));
    }

    /**
     * 新增料仓
     */
    @PreAuthorize("@ss.hasPermi('mes:Stockbin:add')")
    @Log(title = "料仓", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StockBin stockBin)
    {

        StockBin query=new StockBin();
        query.setSbId(stockBin.getSbId());

                if( stockBinService.selectStockBinList(query).isEmpty()==false){
                    return AjaxResult.error("编号已存在");
                }


        return toAjax(stockBinService.insertStockBin(stockBin));
    }

    /**
     * 修改料仓
     */
    @PreAuthorize("@ss.hasPermi('mes:Stockbin:edit')")
    @Log(title = "料仓", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StockBin stockBin)
    {
        return toAjax(stockBinService.updateStockBin(stockBin));
    }

    /**
     * 删除料仓
     */
    @PreAuthorize("@ss.hasPermi('mes:Stockbin:remove')")
    @Log(title = "料仓", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stockBinService.deleteStockBinByIds(ids));
    }
}